﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace kwzx
{
    public partial class logo : Form
    {
        public bool statr = false;
        public DataTable user;
        bool close = false;
        public logo()
        {
            this.Width = 100;
            InitializeComponent();
        }

        private void logo_FormClosed(object sender, FormClosedEventArgs e)
        {

            if (!close)
            {
                statr = true;
                Application.Exit();
            }
            else
            {

            }
        }

        private void logo_Load(object sender, EventArgs e)
        {
            //label3.Parent = pictureBox1;
            label5.Parent = pictureBox1;
            this.Height = panel1.Height + 20;
            this.Width = panel1.Width;
            try
            {
                //string ing = ConfigurationSettings.AppSettings["name"].ToString(); 
                // kwzx.Properties.Settings.Default.
                bool ii = Properties.Settings.Default.nptz;
                textBox1.Text = Properties.Settings.Default.name.ToString();
                textBox2.Text = Properties.Settings.Default.pw.ToString();
                checkBox1.Checked = Properties.Settings.Default.nptz;
                checkBox2.Checked = Properties.Settings.Default.zddl;


            }
            catch (Exception)
            {


            }



            panel1.Dock = DockStyle.Fill;
            this.FormBorderStyle = FormBorderStyle.None;

            panel2.Visible = false;


            if (checkBox2.Checked)
            {
                // button1_Click(sender, e);
            }


            //this.textBox1.Text = "admin";
           // this.textBox2.Text = "a";
        }
        int l = 1;
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (l == 1)
            {
                panel1.Visible = false;
                this.FormBorderStyle = FormBorderStyle.None;
                panel2.Visible = true;
                this.panel2.Top = -1;
                this.panel2.Left = -1;
                this.Height = panel2.Height;
                this.Width = panel2.Width;
                textBox1.Select();
            }
            l++;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("用户名不能为空");
                textBox1.Focus();
                return;
            }
            DataSet1TableAdapters.V_userTableAdapter tuser = new DataSet1TableAdapters.V_userTableAdapter();
            string xm = textBox1.Text.Trim();
            string mm = textBox2.Text.Trim();
            user = tuser.GetDataBy_log(xm, mm);
            if (user.Rows.Count > 0)
            {
                kwzx.Utils.CurrentUser.userName = user.Rows[0]["XM"].ToString();
                kwzx.Utils.CurrentUser.xm = user.Rows[0]["ZSXM"].ToString();  //真实姓名
                kwzx.Utils.CurrentUser.bm = Convert.ToInt32(user.Rows[0]["DEPT_id"].ToString());   //部门id
                Utils.CurrentUser.userid = Convert.ToInt32(user.Rows[0]["user_id"].ToString());

                if (checkBox1.Checked)
                {
                    Properties.Settings.Default["name"] = textBox1.Text;
                    Properties.Settings.Default["pw"] = textBox2.Text;
                    Properties.Settings.Default["nptz"] = checkBox1.Checked;
                    Properties.Settings.Default["zddl"] = checkBox2.Checked;
                    Properties.Settings.Default.Save();
                }
                else
                {
                    Properties.Settings.Default["name"] = "";
                    Properties.Settings.Default["pw"] = "";
                    Properties.Settings.Default["nptz"] = checkBox1.Checked;
                    Properties.Settings.Default["zddl"] = checkBox2.Checked;
                    Properties.Settings.Default.Save();
                }
                //登录成功后

                close = true;
                Close();
                statr = false;
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
                return;

            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.ToString() == "13")
            {
                textBox2.Select();
            }

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.ToString() == "13")
            {
                button1_Click(sender, e);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            PictureBox pb = sender as PictureBox;
            foreach (Control C in pb.Controls)
            {
                if (C is Label)
                {
                    Label L = (Label)C;
                    L.Visible = false;
                    e.Graphics.DrawString(L.Text, L.Font, new SolidBrush(L.ForeColor),
                        new RectangleF(L.Left - pb.Left, L.Top - pb.Top, L.Width, L.Height));
                }
            }

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox1.Checked = true;
                checkBox1.Enabled = false;
            }
            else
            {
                checkBox1.Enabled = true;
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
